﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using COMOSOFT.Class;


namespace COMOSOFT.MatarialManagement.Forms
{
    public partial class PopUpMA : Form
    {
        ClassShare cls = new ClassShare("PopUpMA");
        int MALID, MAID, No, Quantity;
        string OIDs;
        double EstCons, CusCons, ActCons, Wastage;
        string Sizes = null;
        struct SizeNode
        {
            public int OID;
            public int Qty;
            public int[] SBIDs;
        }
        List<SizeNode> ListSize = new List<SizeNode>();   
        public PopUpMA(int _MALID,int _MAID,string _OIDs,int _No)
        {
            InitializeComponent();
            this.MALID = _MALID;
            this.MAID = _MAID;
            this.OIDs = _OIDs;
            this.No = _No;
        }
        #region [Load PopUp]
        private void PopUpMA_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'dataSetMatarial.Supplier' table. You can move, or remove it, as needed.
            this.supplierTableAdapter.Fill(this.dataSetMatarial.Supplier);
            sp_ChooseMASourceTableAdapter.Fill(dataSetMatarial.sp_ChooseMASource,OIDs, MALID);
            sp_ChooseColorTableAdapter.Fill(dataSetMatarial.sp_ChooseColor, MAID);           
        }
        #endregion//

        #region [Show SizeBreakdown]
        private void gridViewPO_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (gridViewPO.FocusedRowHandle >= 0)
            {
                int OID = Convert.ToInt32(gridViewPO.GetFocusedDataRow()["OID"]);
                orderSizeBreakdownTableAdapter.Fill(dataSetMatarial.OrderSizeBreakdown, OID);
            }            
        }
        private void gridViewPO_RowCellClick(object sender, DevExpress.XtraGrid.Views.Grid.RowCellClickEventArgs e)
        {     
            //--
        }
        #endregion

        #region [Choose Size] 
        private void bntSizeOK_Click(object sender, EventArgs e)
        {
            try
            {
                if (gridView_Size.FocusedRowHandle >= 0)
                {
                    int[] selectrow = gridView_Size.GetSelectedRows();
                    if (selectrow.Count() > 0)
                    {
                        SizeNode node = new SizeNode();
                        node.OID = Convert.ToInt32(gridViewPO.GetFocusedDataRow()["OID"]);
                        node.SBIDs = new int[selectrow.Count()];
                        int set = 0;
                        foreach (int row in selectrow)
                        {
                            node.SBIDs[set] = Convert.ToInt32(gridView_Size.GetDataRow(row)["SBID"]);
                            if (set == 0)
                            {
                                Sizes = gridView_Size.GetDataRow(row)["Inseam"].ToString()+ gridView_Size.GetDataRow(row)["Size"].ToString();
                            }
                            else
                            {
                                Sizes += "," + gridView_Size.GetDataRow(row)["Inseam"].ToString()+ gridView_Size.GetDataRow(row)["Size"].ToString();
                            }
                            set++;
                            node.Qty += Convert.ToInt32(gridView_Size.GetDataRow(row)["Qty"]);
                        }                        
                        ListSize.Add(node);
                    }
                    else
                    {
                        Sizes = null;
                    }
                    gridViewPO.SetFocusedRowCellValue("Size", Sizes);
                }               
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
        #endregion       

        #region [Save]
        private void dataNavigator_button_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            switch (e.Button.Tag.ToString())
            {
                case "OK":
                    try
                    {
                        if (gridViewPO.RowCount > 0)
                        {
                            for (int i = 0; i < gridViewPO.RowCount; i++)
                            {
                                if (gridViewPO.GetDataRow(i)["Color"].ToString() != "")
                                {                                    
                                    int OID = Convert.ToInt32(gridViewPO.GetDataRow(i)["OID"]);
                                    int MADID = Convert.ToInt32(gridViewPO.GetDataRow(i)["Color"]);
                                    if (gridViewPO.GetDataRow(i)["EstCons"].ToString() != "")
                                        EstCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["EstCons"]);
                                    if (gridViewPO.GetDataRow(i)["CusCons"].ToString()!="")
                                       CusCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["CusCons"]);
                                    if (gridViewPO.GetDataRow(i)["ActCons"].ToString()!="")
                                       ActCons = Convert.ToDouble(gridViewPO.GetDataRow(i)["ActCons"]);
                                    if (gridViewPO.GetDataRow(i)["Wastage"].ToString()!="")
                                       Wastage = Convert.ToDouble(gridViewPO.GetDataRow(i)["Wastage"]);

                                    if (ListSize.Count > 0)
                                    {
                                        foreach (SizeNode node in ListSize)
                                        {
                                            if (node.OID == OID)
                                            {
                                                Quantity = node.Qty;
                                                DataTable dtb = cls.GetData("sp_MAListDetailInsert", new string[] { "@MALID", "@OID", "@MAID", "@MADID", "@EstCons", "@CusCons", "@ActCons", "@Wastage", "@Quantity", "@Type", "@No" }, new object[] { MALID, OID, MAID, MADID, EstCons, CusCons, ActCons, Wastage, Quantity, "Production", No });
                                                foreach (int sbid in node.SBIDs)
                                                {
                                                   cls.UpdateData("sp_MASizeUsDetailsUpdate", new string[2] { "@SBID", "@MALDID" }, new object[2] { sbid, Convert.ToInt32(dtb.Rows[0][0]) });
                                                }
                                            }
                                        }
                                    }
                                    else
                                    {
                                        DataTable dtb = cls.GetData("sp_MAListDetailInsert", new string[] { "@MALID", "@OID", "@MAID", "@MADID", "@EstCons", "@CusCons", "@ActCons", "@Wastage", "@Quantity", "@Type", "@No" }, new object[] { MALID, OID, MAID, MADID, EstCons, CusCons, ActCons, Wastage, Quantity, "Production", No });
                                    }
                                }
                            }
                        }
                    }
                    catch 
                    {
                        MessageBox.Show("You must choose color or Enter the Cons!", "Error...", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                    finally
                    {
                        this.Close();
                    }
                    break;
                case "Cancel":
                    this.Close();
                    break;
                default :
                    break;
            }
        }
        #endregion      

    }
}
